package com.kehutong.charge.service.impl;

import com.kehutong.charge.entity.ChargeBillCommunity;
import com.kehutong.charge.entity.ChargeReceiptFapiao;
import com.kehutong.charge.service.ChargeFapiaoService;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;

import java.util.List;
import java.util.Objects;

/**
 * @program: kht
 * @description: ChargeFapiaoServiceImpl
 * @author: joneron
 * @create: 2021/7/12
 **/
public class ChargeFapiaoServiceImpl implements ChargeFapiaoService {

    @Inject
    private JdbcSession jdbcSession;

    @Override
    public Page<ChargeReceiptFapiao> findPage(JSONObject jsonObject) {
        FindPage<ChargeReceiptFapiao> find = jdbcSession.findPage(ChargeReceiptFapiao.class)
                .eq("deleted", false)
                .eq(jsonObject,"receiptType")
                .eq(jsonObject,"titleType")
                .eq(jsonObject,"companyNo")
                .eq(jsonObject,"companyName")
                .eq(jsonObject,"personalName")
                .eq(jsonObject,"companyDuty")
                .order("createTime", "desc");

        find.page(jsonObject);
        return find.exe();
    }

    @Override
    public List<ChargeReceiptFapiao> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(ChargeReceiptFapiao.class)
                .eq("deleted", false)
                .eq(jsonObject,"companyNo")
                .eq(jsonObject,"titleType")
                .exe();
    }

    @Override
    public Object save(ChargeReceiptFapiao chargeReceiptFapiao) {

        /**
        List<ChargeReceiptFapiao> list =  jdbcSession.findArray(ChargeReceiptFapiao.class)
                .eq("companyNo",chargeReceiptFapiao.getCompanyNo()).exe();
        if(Objects.nonNull(list)){
            return Result.error(2021,"该企业发票已存在！");
        }*/

        return jdbcSession.insert(chargeReceiptFapiao);
    }

    @Override
    public Object update(ChargeReceiptFapiao chargeReceiptFapiao) {
        return jdbcSession.updateById(chargeReceiptFapiao);
    }

    @Override
    public Object deleteById(String id) {
        ChargeBillCommunity result = Pool.get(ChargeBillCommunity.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }

    @Override
    public Object getObj(JSONObject jsonObject) {
        return jdbcSession.findOne(ChargeReceiptFapiao.class)
                .eq(jsonObject,"companyNo")
                .eq(jsonObject,"personalName")
                .exe();
    }
}
